package com.googlecode.jsimplemarket.wicket.zzz;

//import com.googlecode.jsimplemarket.wicket.DefaultViewPage;
//import com.googlecode.jsimplemarket.model.Magazin;

import com.googlecode.jsimplemarket.model.Absenta;
import com.googlecode.jsimplemarket.model.ZZZ;
import com.googlecode.jsimplemarket.service.GenericService;
import com.googlecode.jsimplemarket.service.ZZZService;
import com.googlecode.jsimplemarket.wicket.pages.DefaultPageTemplate;
import com.googlecode.jsimplemarket.wicket.pages.RaportAbsentePage;
//import com.googlecode.jsimplemarket.service.GenericService;
//import com.googlecode.jsimplemarket.wicket.pages.DefaultViewPage;
//import com.googlecode.jsimplemarket.wicket.SecuredWebSession;
import com.googlecode.jsimplemarket.wicket.zzz.ZZZListPage;
import com.googlecode.jsimplemarket.wicket.zzz.ZZZAddPage;

import java.math.BigDecimal;
import java.util.List;





//import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;
//import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
//import org.apache.wicket.model.Model;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
//import org.apache.wicket.markup.html.form.ChoiceRenderer;
//import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;

import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.GregorianCalendar;
import java.util.Locale;


public class ZZZListPage extends DefaultPageTemplate {
    
	@SpringBean(name = "genericService")
    private GenericService<ZZZ> serviceZzz;
    @SpringBean(name = "zzzService")
    private ZZZService zzzService;    
   
    private Date today = new Date(); // data curenta
    private Date dateS = addDays(today, 0); // data azi resetata la miezul noptii
    private Date dateF = addDays(today, 1); // data maine cu data curenta + o zi
    //numar de zile diferenta intre zilele selectate
    private Long difDates = ((dateF.getTime() - dateS.getTime()) / (1000 * 60 * 60 * 24)); 
    
    private Locale locale = new Locale("ro","RO");
    private NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
    
    public static Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
//        cal.set(Calendar.HOUR_OF_DAY, 0); //doar cand se lucreaza cu 24h format
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.AM_PM, Calendar.AM);
        cal.set(Calendar.MILLISECOND, 0);
                 
        return cal.getTime();
    }

    private BigDecimal difD = BigDecimal.ONE;
    private BigDecimal sold = BigDecimal.ZERO;
    
    private class ZZZView extends ListView<Object[]> {

        public ZZZView(String id, List<Object[]> data) {
            super(id, data);
        }

        @Override
        protected void populateItem(ListItem<Object[]> li) {
            final Object[] object = li.getModelObject();
            int index = li.getIndex() + 1;
            Label lblIndex = new Label("index", index);
            li.add(lblIndex);
            li.add(new Label("denumire", object==null ? "-" : object[0].toString()));
            li.add(new Label("dataAdaugarii", object==null ? "-" : DateFormat.getDateInstance(DateFormat.SHORT, locale).format(object[1])));
            li.add(new Label("valoareTotala", object==null ? "-" : fmt.format(object[2])));
            
                Link delLink = new Link("delLink") {
                    @Override
                    public void onClick() {
                    	
                    	if (object!=null) {
                    		ZZZ zzz = serviceZzz.getById(ZZZ.class, Long.valueOf(object[3].toString()));
                    		serviceZzz.delete(zzz);
                    	}
                    	setResponsePage(new ZZZListPage());
                                       
                    }
                };
                li.add(delLink);
            
        }
    }
    
    public ZZZListPage() {
        
        add(new FeedbackPanel("feedback"));
                        
        final DateTextField dtDataStart = new DateTextField("dataStart", new PropertyModel<Date>(this, "dateS"), "dd.MM.yyyy");
        dtDataStart.add(new DatePicker());
        // dtDataStart.setRequired(true);
        
        final DateTextField dtDataStop = new DateTextField("dataStop", new PropertyModel<Date>(this, "dateF"), "dd.MM.yyyy");
        dtDataStop.add(new DatePicker());
        // dtDataStop.setRequired(true);

        final Label lbPerioada = new Label("lbPerioada", "Astazi");
        add(lbPerioada);
        
        final ZZZView zzzView = new ZZZView("bonuriAzi", zzzService.queryZZZByMagazinAll(dateS, dateF));
        sold = zzzService.queryZZZByMagazinSum(dateS, dateF);
        final Label lblVandAzi = new Label("vanzariAzi", sold==null ? "0,00" : fmt.format(sold));
                
        add(zzzView);
        add(lblVandAzi);
        
        Form<?> form = new Form<Void>("form") {
			@Override
			protected void onSubmit() {
                
                //numar de zile diferenta intre zilele selectate
                difDates = ((dateF.getTime() - dateS.getTime()) / (1000 * 60 * 60 * 24)); 
                
                lbPerioada.setDefaultModelObject(DateFormat.getDateInstance(DateFormat.SHORT, locale).format(dateS) + " - " + DateFormat.getDateInstance(DateFormat.SHORT, locale).format(dateF) + " ( " + difDates  + " zile)" );
                lbPerioada.modelChanged();
                
                difD = new BigDecimal(difDates.toString());
                
                try {                	
                	zzzView.setDefaultModelObject(zzzService.queryZZZByMagazinAll(dateS, dateF));
                	zzzView.modelChanged();
                	sold=zzzService.queryZZZByMagazinSum(dateS, dateF);
                	lblVandAzi.setDefaultModelObject(sold==null ? "0,00" : fmt.format(sold));
                	lblVandAzi.modelChanged();
                	
                } catch (RuntimeException e) {
                	  // depends on language log message and show it in feedbackPanel
                	  error("Nu sunt inregistrari sau intervalul este gresit! <br>" + e.getMessage());
                	  zzzView.setDefaultModelObject(null);
                  	  zzzView.modelChanged();
                  	  lblVandAzi.setDefaultModelObject(null);
                	  lblVandAzi.modelChanged();                 	  
                	  
                }

			}
		};

        form.add(dtDataStart);
        form.add(dtDataStop);
		add(form);
        
        Link addLink = new Link("addLink") {
            @Override
            public void onClick() {
                setResponsePage(new ZZZAddPage());
            }
        };
        add(addLink);
        
    }
}